package com.imay.live.view.custom;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.imay.live.proto.ResultCode;
import com.imay.live.view.refresh.RefreshView;
import com.imay.live.R;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.lcodecore.tkrefreshlayout.footer.LoadingView;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by lzx on 2017/04/25 0025.
 * 兼顾下拉刷新加载界面
 */

public class LoadRefreshViewControl {
    private ImageView progress;
    private TextView load_text;
    private TwinklingRefreshLayout refreshLayout;
    private int DATACOUNT = 20;//最大加载数理
    private int start = 0;
    private boolean loadMoreEnd;//加载到底了
    private List mdatas;
    private Context context;
    private BaseQuickAdapter mAdapter;
    private View load_layout;
    private boolean enableRefresh = true;
    private String error;

    public LoadRefreshViewControl(Context context, BaseQuickAdapter mAdapter, int DATACOUNT) {
        this.context = context;
        this.mAdapter = mAdapter;
        this.DATACOUNT = DATACOUNT;
        if (mAdapter != null)
            mdatas = mAdapter.getData();
    }

    public void setmAdapter(BaseQuickAdapter mAdapter) {
        this.mAdapter = mAdapter;
        mdatas = mAdapter.getData();
    }

    /**
     * 必须先加载UI
     */
    public RecyclerView addLoadView(FrameLayout container, RefreshListenerAdapter refreshListenerAdapter, boolean enableRefresh) {
//        RecyclerView recyclerView = new RecyclerView(context);
        refreshLayout = (TwinklingRefreshLayout) container.findViewById(R.id.trl_refresh);
        RecyclerView recyclerView = (RecyclerView) container.findViewById(R.id.trl_recyclervew);
//        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
//        container.addView(refreshLayout, 0, params);
//        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
//        recyclerView.setOverScrollMode(View.OVER_SCROLL_NEVER);
//        refreshLayout.addView(recyclerView, layoutParams);
        load_layout = container.findViewById(R.id.load_layout);
        this.enableRefresh = enableRefresh;
        if (enableRefresh) {
            RefreshView headerView = new RefreshView(context);
            refreshLayout.setHeaderView(headerView);
        }
        LoadingView bottomProgressView = new LoadingView(context);
        refreshLayout.setBottomView(bottomProgressView);
        refreshLayout.setEnableLoadmore(false);
        refreshLayout.setEnableOverScroll(false);
        refreshLayout.setOnRefreshListener(refreshListenerAdapter);

        if (load_layout == null) {
            load_layout = LayoutInflater.from(context).inflate(R.layout.load_layout, container, false);
            container.addView(load_layout);
        }
        initLoadLayout();
        return recyclerView;
    }

    /**
     * 必须先加载UI
     */
    public RecyclerView addLoadView(FrameLayout container, RefreshListenerAdapter refreshListenerAdapter) {
        return addLoadView(container, refreshListenerAdapter, true);
    }

    /**
     * 初始化参数
     */
    private void initLoadLayout() {
        refreshLayout.setEnableRefresh(enableRefresh);
        progress = (ImageView) load_layout.findViewById(R.id.progress_icon);
        load_text = (TextView) load_layout.findViewById(R.id.load_text);
        error = context.getResources().getString(R.string.str_nodata);
        load_layout.setVisibility(View.VISIBLE);
        ((AnimationDrawable) progress.getDrawable()).start();
    }

    public void loadError(String error) {
        if (mAdapter != null) {
            mdatas.clear();
            mAdapter.notifyDataSetChanged();
        }
        load_text.setVisibility(View.VISIBLE);
        load_layout.setVisibility(View.VISIBLE);
        load_text.setText(error);
        progress.clearAnimation();
        progress.setImageResource(R.mipmap.wushuju);
        refreshLayout.finishRefreshing();
        refreshLayout.finishLoadmore();
        refreshLayout.setEnableRefresh(enableRefresh);
    }

    public void loadError() {
        loadError(context.getResources().getString(R.string.str_loaderror));
    }

    public void setError(String error) {
        this.error = error;
    }

    /**
     * data 是新增数据
     * @param data
     * @param result
     */
    public void refreshLoadView(ArrayList<? extends MultiItemEntity> data, int result) {
        progress.clearAnimation();
        load_layout.setVisibility(View.GONE);
        mdatas.clear();
        if (result == ResultCode.CodeSuccess && data != null) {
            refreshLayout.setVisibility(View.VISIBLE);
            if (data.size() == 0) {
                loadMoreEnd = true;
            } else {
                loadMoreEnd = false;
                start = DATACOUNT;
            }
            if (data.isEmpty()) {
                loadError(error);
            } else {
                mdatas.addAll(data);
            }
            refreshLayout.setEnableLoadmore(!loadMoreEnd);
        } else {
            loadError(error);
        }
        mAdapter.notifyDataSetChanged();
        refreshLayout.setEnableRefresh(enableRefresh);
        refreshLayout.finishRefreshing();
    }

    /**
     * data 是新增数据
     * @param data
     * @param result
     */
    public void refreshLoadView(ArrayList<? extends MultiItemEntity> data, int result,boolean loadMoreEnd) {
        progress.clearAnimation();
        load_layout.setVisibility(View.GONE);
        mdatas.clear();
        if (result == ResultCode.CodeSuccess && data != null) {
            refreshLayout.setVisibility(View.VISIBLE);
            if (data.size() == 0) {
                loadMoreEnd = true;
            } else {
                loadMoreEnd = false;
                start = DATACOUNT;
            }
            if (data.isEmpty()) {
                loadError(error);
            } else {
                mdatas.addAll(data);
            }
            refreshLayout.setEnableLoadmore(loadMoreEnd);
        } else {
            loadError(error);
        }
        mAdapter.notifyDataSetChanged();
        refreshLayout.setEnableRefresh(enableRefresh);
        refreshLayout.finishRefreshing();
    }


    public void setEnableLoadmore(boolean ismore) {
        if (!loadMoreEnd) {
            refreshLayout.setEnableLoadmore(ismore);
        }
    }

    public void moreLoadView(ArrayList<? extends MultiItemEntity> data, int result) {
        if (result == ResultCode.CodeSuccess && data != null) {
            if (data.size() == 0) {
                loadMoreEnd = true;
            } else {
                start = start + DATACOUNT;
                loadMoreEnd = false;
            }
            mdatas.addAll(data);
            refreshLayout.setEnableLoadmore(!loadMoreEnd);
            mAdapter.notifyDataSetChanged();
        } else {
            refreshLayout.setEnableLoadmore(false);
        }
        refreshLayout.finishLoadmore();
    }

    public void morePasteLoadView(ArrayList<? extends MultiItemEntity> data, int result) {
        if (result == ResultCode.CodeSuccess && data != null) {
            mdatas.addAll(data);
            refreshLayout.setEnableLoadmore(true);
            mAdapter.notifyDataSetChanged();
        }
        refreshLayout.finishLoadmore();
    }

    public int getDATACOUNT() {
        return DATACOUNT;
    }

    public int getStart() {
        return start;
    }
}
